package com.aistar.service.impl;

import com.aistar.mapper.ProductImagesMapper;
import com.aistar.mapper.ProductMapper;
import com.aistar.mapper.ProductSpecMapper;
import com.aistar.mapper.SeckillMapper;
import com.aistar.pojo.*;
import com.aistar.service.SeckillDetailService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SeckillDetailServiceImpl implements SeckillDetailService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImagesMapper productImagesMapper;

    @Autowired
    private SeckillMapper seckillMapper;

    @Autowired
    private ProductSpecMapper productSpecMapper;

    @Override
    @Cacheable(value ="goods",key = "#proId")
    public ServerResponse getGoodsById(Integer proId) {
        Product product = productMapper.selectByPrimaryKey(proId);
        return ServerResponse.getDataSuccess(product);
    }

    @Override
    @Cacheable(value ="seckill",key = "#proId")
    public ServerResponse getSeckillByGId(Integer proId) {
        SeckillExample seckillExample = new SeckillExample();
        SeckillExample.Criteria criteria = seckillExample.createCriteria();
        criteria.andProductIdEqualTo(proId);
        List<Seckill> seckillList = seckillMapper.selectByExample(seckillExample);
        if (seckillList!=null){
            return ServerResponse.getDataSuccess(seckillList.get(0));
        }
        return ServerResponse.getDataFailed(MessageUtil.NO_DATA);
    }


    @Override
    @Cacheable(value ="goodsImg",key = "#proId")
    public ServerResponse getGoodsImgByGId(Integer proId) {
        ProductImagesExample productImagesExample = new ProductImagesExample();
        productImagesExample.createCriteria().andProductImagesStatusEqualTo(1)
                .andProductIdEqualTo(proId);
        List<ProductImages> productImagesList = productImagesMapper.selectByExample(productImagesExample);
        for (ProductImages productImages:productImagesList){
            System.out.println("productImages:"+productImages);
        }
        if (productImagesList !=null && productImagesList.size()>0)
            return ServerResponse.getDataSuccess(productImagesList);
        return ServerResponse.getDataFailed("该商品没有图片");
    }

    @Override
    @Cacheable(value ="spec",key = "#proId")
    public ServerResponse getSepecByGId(Integer proId) {
        ProductSpecExample productSpecExample =  new ProductSpecExample();
        productSpecExample.createCriteria().andProductIdEqualTo(proId);
        List<ProductSpec> productSpecList = productSpecMapper.selectByExample(productSpecExample);
        if (productSpecList !=null && productSpecList.size()>0)
            return ServerResponse.getDataSuccess(productSpecList.get(0));
        return ServerResponse.getDataFailed(MessageUtil.NO_DATA);

    }
}

